package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyComment;
import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.query.CommentQuery;
import com.cd.javacxy.common.model.vo.AjaxResponse;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.base.BaseController;
import com.cd.javacxy.module.web.service.CxyCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * CxyComment的路由接口服务
 *
 * @author yanghy
 */
@RestController
@Slf4j
public class CxyCommentController extends BaseController {

    private CxyCommentService cxyCommentService;

    @Autowired
    public void setCxyCommentService(CxyCommentService cxyCommentService) {
        this.cxyCommentService = cxyCommentService;
    }


    @RequestMapping("/comment/list")
    public AjaxResponse<PageResult<CxyComment>> loadComment(HttpSession session, CommentQuery commentQuery) {
        AjaxResponse<PageResult<CxyComment>> ajaxResponse = AjaxResponse.<PageResult<CxyComment>>builder().build();
        try {
            PageResult<CxyComment> pageResult = this.cxyCommentService.findCommentByPage(commentQuery);
            ajaxResponse.setData(pageResult);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (Exception e) {
            log.error("加载评论出错", e);
            ajaxResponse.setErrorMsg("加载评论出错");
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
        }
        return ajaxResponse;
    }

    @RequestMapping("/comment/publish")
    public AjaxResponse<Object> addComment(HttpSession session, CxyComment comment) {
        AjaxResponse<Object> ajaxResponse = AjaxResponse.builder().build();
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionUser == null) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg("请先登录");
            return ajaxResponse;
        }
        this.setUserBaseInfo(CxyComment.class, comment, session);
        try {
            this.cxyCommentService.addComment(comment);
            ajaxResponse.setData(comment);
            ajaxResponse.setCode(ResponseCode.SUCCESS.getCode());
        } catch (BussinessException e) {
            ajaxResponse.setCode(ResponseCode.BUSSINESSERROR.getCode());
            ajaxResponse.setErrorMsg(e.getLocalizedMessage());
            log.error("{}评论出错", sessionUser.getUserName());
        } catch (Exception e) {
            ajaxResponse.setCode(ResponseCode.SERVERERROR.getCode());
            ajaxResponse.setErrorMsg("服务器出错");
            log.error("{}评论出错", sessionUser.getUserName(), e);
        }
        return ajaxResponse;
    }
}
